/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.dao.lineprofile.impl.dom4j;

import hxg.portmgr.core.dao.DaoSource;
import hxg.portmgr.core.dao.LineProfileDAO;
import hxg.portmgr.core.dao.lineprofile.impl.AbsLineProfileDAO;
import hxg.portmgr.core.device.Device;
import hxg.portmgr.core.device.dslam.LineProfile;
import hxg.portmgr.core.oper.snmp.param.AdslParam;
import java.io.*;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author hxg
 */
public class LineProfileDaoDom4jImpl extends AbsLineProfileDAO implements LineProfileDAO {

    File f = new File(".\\data\\LineProfileDom4j.xml");

    @Override
    public void save() {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("Devices");
        for (long devId : mp.keySet()) {
            Element deviceEles = root.addElement("Device");
            List<LineProfile> porfs = mp.get(devId);
            //Element devele = root.addElement("Device");
            Device dev = DaoSource.getDeviceDao().getDeviceById(devId);
            deviceEles.addAttribute("id", Long.toString(devId));
            deviceEles.addAttribute("name", dev.getChsName());
            deviceEles.addAttribute("ip", dev.getIp());
            for (LineProfile prof : porfs) {
                Element profele = deviceEles.addElement("LineProfile");
                profele.addAttribute("name", prof.getName());
                profele.addAttribute("index", prof.getIndex());
                profele.addAttribute("mode", Integer.toString(prof.getRateModeIntValue()));
                for (AdslParam param : prof.getValueMap().keySet()) {
                    Element valueEle = profele.addElement(param.toString());
                    valueEle.addAttribute("value", Integer.toString(prof.getValue(param)));
                }
            }
        }
        try {
            OutputFormat opf = OutputFormat.createPrettyPrint();
            opf.setLineSeparator("\r\n");//这是为了换行操作  
            XMLWriter outPut = new XMLWriter(new FileWriter(f, false), opf);
            outPut.write(doc);
            outPut.close();
        } catch (IOException ex) {
            Logger.getLogger(LineProfileDaoDom4jImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void load() {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            for (Element devEle : (List<Element>) doc.getRootElement().elements("Device")) {
                long devId = Long.parseLong(devEle.attributeValue("id"));
                System.out.println(devId);
                List<LineProfile> profs = new ArrayList<>();
                for (Element profEle : (List<Element>) devEle.elements("LineProfile")) {
                    LineProfile prof = new LineProfile();
                    prof.setName(profEle.attributeValue("name"));
                    prof.setIndex(profEle.attributeValue("index"));
                    prof.setRateMode(Integer.parseInt(profEle.attributeValue("mode")));
                    EnumMap<AdslParam, Integer> enummp = new EnumMap<>(AdslParam.class);
                    for (Element paramEle : (List<Element>) profEle.elements()) {
                        enummp.put(AdslParam.valueOf(paramEle.getQualifiedName()), Integer.parseInt(paramEle.attributeValue("value")));
                    }
                    prof.addProfileValueMap(enummp);
                    profs.add(prof);
                }
                mp.put(devId, profs);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(LineProfileDaoDom4jImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
